﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections;

namespace IHISCETeim.Library
{
    /// <summary>
    /// 提供操作DropDowmList控件常用的方法
    /// </summary>
    public static class DropDownListExt
    {
        #region 为DropDownList控件绑定数据

        /// <summary>
        /// 为DropDowmList控件绑定数据
        /// </summary>
        /// <param name="dropdownlist">DropDownList控件</param>
        /// <param name="dt">要绑定的表(id,text)</param>
        /// <param name="textField">要绑定的Text字段名</param>
        /// <param name="valueField">要绑定的Value字段名</param>
        public static void BindData(this DropDownList dropdownlist, DataTable dt, string textField, string valueField)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    DataRow dr = dt.Rows[i];
                    ListItem item = new ListItem(dr[textField].ToString(), dr[valueField].ToString());
                    dropdownlist.Items.Add(item);
                }
            }
        }

        /// <summary>
        /// 为DropDowmList控件绑定数据
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="ddl">DropDownList控件</param>
        /// <param name="list">数据源</param>
        /// <param name="dataField">要绑定的Text字段名</param>
        /// <param name="dataValue">要绑定的Value字段名</param>
        public static void BindData<T>(this DropDownList ddl, List<T> list, string dataField, string dataValue)
        {
            if (list != null && list.Count > 0)
            {
                ddl.DataSource = list;
                ddl.DataTextField = dataField;
                ddl.DataValueField = dataValue;
                ddl.DataBind();
            }
        }

        /// <summary>
        /// 为DropDowmList控件绑定数据
        /// </summary>
        /// <typeparam name="T">数据源类型</typeparam>
        /// <param name="ddl">DropDownList控件</param>
        /// <param name="list">数据源</param>
        /// <param name="dataField">要绑定的Text字段名</param>
        /// <param name="dataValue">要绑定的Value字段名</param>
        /// <param name="firstItemStr">第一项的Text值</param>
        public static void BindData<T>(this DropDownList ddl, List<T> list, string dataField, string dataValue, string firstItemStr)
        {
            ddl.BindData<T>(list, dataField, dataValue);
            ddl.Items.Insert(0, new ListItem(firstItemStr, "0"));
        }

        /// <summary>
        /// 为DropDowmList控件绑定树型的数据
        /// </summary>
        /// <param name="dropdownlist">DropDownList控件</param>
        /// <param name="dt">数据表</param>
        /// <param name="textField">要绑定到DropDownList控件Text属性的字段名</param>
        /// <param name="valueField">要绑定到DropDownList控件Value属性的字段名</param>
        /// <param name="parentField">父级字段名</param>
        /// <param name="parentFieldValue">父级字段值</param>
        /// <param name="format">显示格式，如："|"，则显示为"|---，|------"</param>
        public static void BindData(this DropDownList dropdownlist, DataTable dt, string textField, string valueField, string parentField, string parentFieldValue, string format)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow[] drs = dt.Select(parentField + "=" + parentFieldValue);
                for (int i = 0; i < drs.Length; i++)
                {
                    format += "---";
                    ListItem item = new ListItem(format + drs[i][textField].ToString(), drs[i][valueField].ToString());
                    dropdownlist.Items.Add(item);
                    BindData(dropdownlist, dt, textField, valueField, parentField, drs[i][valueField].ToString(), format);
                    format = format.Substring(0, format.Length - 3);
                }
            }
        }

        /// <summary>
        /// 为DropDowmList控件绑定可指定深度的树型数据
        /// </summary>
        /// <param name="dropdownlist">DropDownList控件</param>
        /// <param name="dt">数据表</param>
        /// <param name="textField">要绑定到DropDownList控件Text属性的字段名</param>
        /// <param name="valueField">要绑定到DropDownList控件Value属性的字段名</param>
        /// <param name="parentField">父级字段名</param>
        /// <param name="parentFieldValue">父级字段值</param>
        /// <param name="format">显示格式，如："|"，则显示为"|---，|------"</param>
        /// <param name="level">树型数据的深度，0或1表示第一级，2表示第二级，3表示第三级，依次类推</param>
        /// <returns>返回0</returns>
        public static int BindData(this DropDownList dropdownlist, DataTable dt, string textField, string valueField, string parentField, string parentFieldValue, string format, int level)
        {
            int curLevel = 0;
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow[] drs = dt.Select(parentField + "=" + parentFieldValue);
                for (int i = 0; i < drs.Length; i++)
                {
                    format += "---";
                    curLevel += 1;//当前深度
                    ListItem item = new ListItem(format + drs[i][textField].ToString(), drs[i][valueField].ToString());
                    dropdownlist.Items.Add(item);
                    if (curLevel < level)
                    {
                        curLevel += BindData(dropdownlist, dt, textField, valueField, parentField, drs[i][valueField].ToString(), format, level - 1);
                    }
                    curLevel -= 1;//
                    format = format.Substring(0, format.Length - 3);
                }
            }
            return curLevel;
        }

        /// <summary>
        /// 为DropDowmList控件绑定枚举类型的数据
        /// </summary>
        /// <param name="dropdownlist">DropDowmList控件</param>
        /// <param name="enumType">枚举类型</param>
        /// <param name="isBindIntData">DropDowmList控件的Value属性的值是否为枚举的整数值</param>
        public static void BindData(this DropDownList dropdownlist, Type enumType, bool isBindIntData)
        {
            foreach (int i in Enum.GetValues(enumType))
            {
                if (isBindIntData)
                {
                    ListItem item = new ListItem(Enum.GetName(enumType, i), i.ToString());
                    dropdownlist.Items.Add(item);
                }
                else
                {
                    ListItem item = new ListItem(Enum.GetName(enumType, i));
                    dropdownlist.Items.Add(item);
                }
            }
        }

        #endregion

        #region 得到DropDowmList控件中选中项的值

        /// <summary>
        /// 得到DropDowmList控件中选中项的Value属性值
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="dropdownlist">DropDowmList控件</param>
        /// <param name="defaultValue">如果没有选中项，就返回这个默认值</param>
        /// <returns>返回DropDowmList控件中选中项的Value属性值</returns>
        public static T GetSelectedValue<T>(this DropDownList dropdownlist, T defaultValue)
        {
            if (dropdownlist.SelectedItem != null)
            {
                return dropdownlist.SelectedItem.Value.ConvertToType<T>(defaultValue);
            }
            else
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// 得到DropDowmList控件中选中项的Text属性值
        /// </summary>
        /// <param name="dropdownlist">DropDowmList控件</param>
        /// <param name="defaultValue">如果没有选中项，就返回这个默认值</param>
        /// <returns>返回DropDowmList控件中选中项的Text属性值</returns>
        public static string GetSelectedText(this DropDownList dropdownlist, string defaultValue)
        {
            if (dropdownlist.SelectedItem != null)
            {
                return dropdownlist.SelectedItem.Text;
            }
            else
            {
                return defaultValue;
            }
        }

        #endregion

        #region 根据指定的值，从DropDowmList控件中选取对应的项

        /// <summary>
        /// 根据指定的value值，从控件中选取对应的项
        /// </summary>
        /// <param name="dropdownlist">DropDowmList控件</param>
        /// <param name="val">value属性值</param>
        public static void SelectByValue(this DropDownList dropdownlist, string val)
        {
            if (val == null)
            {
                return;
            }
            if (dropdownlist.Items != null && dropdownlist.Items.Count > 0)
            {
                foreach (ListItem li in dropdownlist.Items)
                {
                    li.Selected = false;
                }
            }
            ListItem item = dropdownlist.Items.FindByValue(val);
            if (item != null)
            {
                item.Selected = true;
            }
        }

        /// <summary>
        /// 根据指定的Text值，从控件中选取对应的项
        /// </summary>
        /// <param name="dropdownlist">DropDowmList控件</param>
        /// <param name="val">value属性值</param>
        public static void SelectByText(this DropDownList dropdownlist, string val)
        {
            if (val == null)
            {
                return;
            }
            if (dropdownlist.Items != null && dropdownlist.Items.Count > 0)
            {
                foreach (ListItem li in dropdownlist.Items)
                {
                    li.Selected = false;
                }
            }
            ListItem item = dropdownlist.Items.FindByText(val);
            if (item != null)
            {
                item.Selected = true;
            }
        }

        #endregion
    }
}
